﻿/*-----------------------------------/
/* 
/* 定义全局用户信息
/* Create By.Ben 20181211
/* 
/*----------------------------------*/
var GlobalUser = (function () {

    /*-----------------------------------/
    /* 全局用户信息
    /* 由于涉及跨页，存放在LocalStorage
    /*----------------------------------*/
    var userKey = "GLOBALUSERINFO";
    var userInfo = {};
    
    var setUserInfo = function (info) {
        localStorage.removeItem(userKey);
        localStorage.setItem(userKey, JSON.stringify(info));
        this.userInfo = info;
    };

    var getUserInfo = function () {
        this.userInfo = JSON.parse(localStorage.getItem(userKey));
        if (this.userInfo == null) {
            //如果找不到用户登入信息，则跳转登入页
            RedirectLogin();
        }
        //console.log(JSON.stringify(this.userInfo));
        return this.userInfo;
    }

    var hasGroup = function (groupID) {
        var user = getUserInfo();
        for (var idx in user.UserGroupMappings) {
            if (user.UserGroupMappings[idx].GroupID == groupID)
                return true;
        }
        return false;
    }

    /*-----------------------------------/
    /* 将所有要外露的方法声明在此，才可以调用
    /*----------------------------------*/
    return {
        setUserInfo: setUserInfo,
        getUserInfo: getUserInfo,
        hasGroup: hasGroup
    };

})();

/*-----------------------------------/
/* 
/* 定义全局数据字典信息
/* Create By.Ben 20181211
/* 
/*----------------------------------*/
var GlobalDictionary = (function () {

    /*-----------------------------------/
    /* 数据字典
    /*----------------------------------*/
    var equipmentStatusKey = "EQUIPMENT_STATUS";
    var equipmentTypeKey = "EQUIPMENT_TYPE";
    var equipmentAssignerKey = "EQUIPMENT_ASSIGNER";
    var equipmentManagerKey = "EQUIPMENT_MANAGER";
    var equipmentWarnDaysKey = "EQUIPMENT_WARN_DAYS";
    var equipmentPeriodDaysKey = "EQUIPMENT_PERIOD_DAYS";
    var measureTypeKey = "MEASURE_TYPE";
    var maintainTypeKey = "MAINTAIN_TYPE"
    var maintainStatusKey = "MAINTAIN_STATUS";;
    var msaTypeKey = "MSA_TYPE";

    var trueOrFalseKey = "TRUE_OR_FALSE";
    var approvalStatusKey = "APPROVAL_STATUS";
    var approvalSectionStatusKey = "APPROVAL_SECTION_STATUS";

    var moldsStatusKey = "MOLDS_STATUS";

    //设备状态数据字典
    var SetEquipmentStatusDictionary = function () {
        localStorage.removeItem(equipmentStatusKey);
        $.ajax({
            async: false,
            type: "GET",
            url: "/System/GetSysDictionaryByName?DictionaryName=EquipmentStatus&v=" + Math.random(),
            success: function (res) {
                if (res.status == "Success") {
                    localStorage.setItem(equipmentStatusKey, JSON.stringify(res.data));
                }
            }
        });
    }
    var GetEquipmentStatusDictionary = function () { return GetSysDictionary(equipmentStatusKey); }

    //-----------------------------------------------------------------------------------------------------
    //模具系统的状态数据字典
    var SetMoldsStatusDictionary = function () {
        localStorage.removeItem(moldsStatusKey);
        $.ajax({
            async: false,
            type: "GET",
            url: "/System/GetSysDictionaryByName?DictionaryName=MoldsStatus&v=" + Math.random(),
            success: function (res) {
                if (res.status == "Success") {
                    localStorage.setItem(moldsStatusKey, JSON.stringify(res.data));
                }
            }
        });
    }          
    var GetMoldsStatusDictionary = function () { return GetSysDictionary(moldsStatusKey); }

    //------------------------------------------------------------------------------------------------------


    //设备类型数据字典
    var SetEquipmentTypeDictionary = function () {
        $.ajax({
            async: false,
            type: "GET",
            url: "/Equipment/GetEquipmentTypeDictionary?v=" + Math.random(),
            success: function (res) {
                if (res.status == "Success") {
                    localStorage.setItem(equipmentTypeKey, JSON.stringify(res.data));
                }
            }
        });
    }
    var GetEquipmentTypeDictionary = function () { return GetSysDictionary(equipmentTypeKey); }

    //设备负责人数据字典
    var SetEquipmentAssignerDictionary = function () {
        $.ajax({
            async: false,
            type: "GET",
            url: "/Equipment/ListEquipmentAssigners?v=" + Math.random(),
            success: function (res) {
                if (res.status == "Success") {
                    localStorage.setItem(equipmentAssignerKey, JSON.stringify(res.data));
                }
            }
        });
    }
    var GetEquipmentAssignerDictionary = function () { return GetSysDictionary(equipmentAssignerKey); }

    //设备管理人数据字典
    var SetEquipmentManagerDictionary = function () {
        $.ajax({
            async: false,
            type: "GET",
            url: "/Equipment/ListEquipmentManagers?v=" + Math.random(),
            success: function (res) {
                if (res.status == "Success") {
                    localStorage.setItem(equipmentManagerKey, JSON.stringify(res.data));
                }
            }
        });
    }
    var GetEquipmentManagerDictionary = function () { return GetSysDictionary(equipmentManagerKey); }

    //设备预警日数数据字典
    var SetEquipmentWarnDaysDictionary = function () { SetSysDictionary("EquipmentWarnDays", equipmentWarnDaysKey); }
    var GetEquipmentWarnDaysDictionary = function () { return GetSysDictionary(equipmentWarnDaysKey); }

    //设备周期日数数据字典
    var SetEquipmentPeriodDaysDictionary = function () { SetSysDictionary("EquipmentPeriodDays", equipmentPeriodDaysKey); }
    var GetEquipmentPeriodDaysDictionary = function () { return GetSysDictionary(equipmentPeriodDaysKey); }

    //设备计量类型数据字典
    var SetMeasureTypeDictionary = function () { SetSysDictionary("MeasureType", measureTypeKey); }
    var GetMeasureTypeDictionary = function () { return GetSysDictionary(measureTypeKey); }

    //设备保养类型数据字典
    var SetMaintainTypeDictionary = function () { SetSysDictionary("MaintainType", maintainTypeKey); }
    var GetMaintainTypeDictionary = function () { return GetSysDictionary(maintainTypeKey); }

    //设备保养状态数据字典
    var SetMaintainStatusDictionary = function () { SetSysDictionary("MaintainStatus", maintainStatusKey); }
    var GetMaintainStatusDictionary = function () { return GetSysDictionary(maintainStatusKey); }

    //设备Msa类型数据字典
    var SetMsaTypeDictionary = function () { SetSysDictionary("MsaType", msaTypeKey); }
    var GetMsaTypeDictionary = function () { return GetSysDictionary(msaTypeKey); }

    //是与非状态数据字典
    var SetTrueOrFalseDictionary = function () { SetSysDictionary("TrueOrFalseChs", trueOrFalseKey); }
    var GetTrueOrFalseDictionary = function () { return GetSysDictionary(trueOrFalseKey); }

    //表单签核状态数据字典
    var SetApprovalStatusDictionary = function () { SetSysDictionary("ApprovalStatus", approvalStatusKey); }
    var GetApprovalStatusDictionary = function () { return GetSysDictionary(approvalStatusKey); }

    //表单签核节点状态数据字典
    var SetApprovalSectionStatusDictionary = function () { SetSysDictionary("ApprovalSectionStatus", approvalSectionStatusKey) }
    var GetApprovalSectionStatusDictionary = function () { return GetSysDictionary(approvalSectionStatusKey); }

    //查询系统数据字典并将数据保存到localStorage
    var SetSysDictionary = function (dictionaryName, localStorageKey) {
        $.ajax({
            async: false,
            type: "GET",
            url: "/System/GetSysDictionaryByName?DictionaryName=" + dictionaryName + "&v=" + Math.random(),
            success: function (res) {
                if (res.status == "Success") {
                    localStorage.setItem(localStorageKey, JSON.stringify(res.data));
                }
            }
        })
    }

 
    //根据Key值获取localStorage的Value值
    var GetSysDictionary = function (localStorageKey) {
        return JSON.parse(localStorage.getItem(localStorageKey)); 
    }

    var SetGlobalDictionary = function () {
        SetEquipmentStatusDictionary();
        SetEquipmentTypeDictionary();
        SetEquipmentAssignerDictionary();
        SetEquipmentManagerDictionary();
        SetEquipmentWarnDaysDictionary();
        SetEquipmentPeriodDaysDictionary();
        SetMeasureTypeDictionary();
        SetMaintainTypeDictionary();
        SetMaintainStatusDictionary();
        SetMsaTypeDictionary();
        SetTrueOrFalseDictionary();
        SetApprovalStatusDictionary();
        SetApprovalSectionStatusDictionary();

        SetMoldsStatusDictionary();
    }

    /*-----------------------------------/
    /* 将所有要外露的方法声明在此，才可以调用
    /*----------------------------------*/
    return {
        SetGlobalDictionary: SetGlobalDictionary,

        SetEquipmentStatusDictionary: SetEquipmentStatusDictionary,
        SetEquipmentTypeDictionary: SetEquipmentTypeDictionary,
        SetEquipmentAssignerDictionary: SetEquipmentAssignerDictionary,
        SetEquipmentManagerDictionary: SetEquipmentManagerDictionary,
        SetEquipmentWarnDaysDictionary: SetEquipmentWarnDaysDictionary,
        SetEquipmentPeriodDaysDictionary: SetEquipmentPeriodDaysDictionary,
        SetMeasureTypeDictionary: SetMeasureTypeDictionary,
        SetMaintainTypeDictionary: SetMaintainTypeDictionary,
        SetMaintainStatusDictionary: SetMaintainStatusDictionary,
        SetMsaTypeDictionary: SetMsaTypeDictionary,
        SetTrueOrFalseDictionary: SetTrueOrFalseDictionary,
        SetApprovalStatusDictionary: SetApprovalStatusDictionary,
        SetApprovalSectionStatusDictionary: SetApprovalSectionStatusDictionary,

        SetMoldsStatusDictionary: SetMoldsStatusDictionary,

        GetEquipmentStatusDictionary: GetEquipmentStatusDictionary,
        GetEquipmentTypeDictionary: GetEquipmentTypeDictionary,
        GetEquipmentAssignerDictionary: GetEquipmentAssignerDictionary,
        GetEquipmentManagerDictionary: GetEquipmentManagerDictionary,
        GetEquipmentWarnDaysDictionary: GetEquipmentWarnDaysDictionary,
        GetEquipmentPeriodDaysDictionary: GetEquipmentPeriodDaysDictionary,
        GetMeasureTypeDictionary: GetMeasureTypeDictionary,
        GetMaintainTypeDictionary: GetMaintainTypeDictionary,
        GetMaintainStatusDictionary: GetMaintainStatusDictionary,
        GetMsaTypeDictionary: GetMsaTypeDictionary,
        GetTrueOrFalseDictionary: GetTrueOrFalseDictionary,
        GetApprovalStatusDictionary: GetApprovalStatusDictionary,
        GetApprovalSectionStatusDictionary: GetApprovalSectionStatusDictionary,

        GetMoldsStatusDictionary: GetMoldsStatusDictionary
    };

})();


/*-----------------------------------/
/* 
/* 定义全局菜单
/* Create By.Ben 20190104
/* 
/*----------------------------------*/
var GlobalMenu = (function () {

    /*-----------------------------------/
    /* 系统菜单
    /*----------------------------------*/
    var systemMenus = null;

    //获得菜单
    var GetSystemMenus = function () {
        if (systemMenus == null) {
            $.ajax({
                async: false,
                type: "GET",
                url: "/System/GetSystemMenus?v=" + Math.random(),
                success: function (res) {
                    if (res.status == "Success") {
                        systemMenus = res.data;
                    } else {
                        toastr.error(res.message);

                    }
                },
                error: function (err) { console.log(err); }
            });
        }
        return systemMenus;
    }
    
    /*-----------------------------------/
    /* 将所有要外露的方法声明在此，才可以调用
    /*----------------------------------*/
    return {
        GetSystemMenus: GetSystemMenus
    };

})();